package com.company.project.handler;

import com.company.project.builder.wx.outxml.ImageBuilder;
import com.company.project.builder.wx.outxml.NewsBuilder;
import com.company.project.builder.wx.outxml.TextBuilder;
import com.company.project.builder.wx.outxml.VoiceBuilder;
import com.company.project.utils.JsonUtils;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutNewsMessage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Binary Wang(https://github.com/binarywang)
 */
@Component
public class MsgHandler extends AbstractHandler {

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                    Map<String, Object> context, WxMpService weixinService,
                                    WxSessionManager sessionManager) {

        if (!wxMessage.getMsgType().equals(WxConsts.XML_MSG_EVENT)) {
            //TODO 可以选择将消息保存到本地
        }

        //当用户输入关键词如“你好”，“客服”等，并且有客服在线时，把消息转发给在线客服
        try
        {
            String content = wxMessage.getContent();
            if (StringUtils.startsWithAny(content , "你好", "客服")
                    && weixinService.getKefuService().kfOnlineList()
                    .getKfOnlineList().size() > 0)
            {
                return WxMpXmlOutMessage.TRANSFER_CUSTOMER_SERVICE()
                        .fromUser(wxMessage.getToUser())
                        .toUser(wxMessage.getFromUser()).build();
            }


            if("文本".equals(content))
            {
                return new TextBuilder().build(content, wxMessage);
            }

            if("图片".equals(content))
            {
                String mediaId = "TVe2YXZ50_-YwWk0kVZBlkyFOGAl8bJfm17sOxTRq8NGFbb7f5vf56K3uTzZ6N0f";
                return  new ImageBuilder().build(mediaId , wxMessage);
            }

            if("语音".equals(content))
            {
                String mediaId = "wFD5g6D1fWYF7ImBhUl_3pLE_tS3rHQSuGA6vWhPwmS2NOeKap_TDwBwTl87u1H6";
                return  new VoiceBuilder().build(mediaId , wxMessage);
            }
            if("图文".equals(content))
            {
                List<WxMpXmlOutNewsMessage.Item> articles = new ArrayList<>();

                WxMpXmlOutNewsMessage.Item itemOne = new WxMpXmlOutNewsMessage.Item();
                itemOne.setTitle("hi how are you");
                itemOne.setDescription("this is a demo demo");
                itemOne.setPicUrl("https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1510307158&di=258b895c1f6840f2b2af3802f6493b66&src=http://imgsrc.baidu.com/imgad/pic/item/9213b07eca806538025ec1349ddda144ad348264.jpg");
                itemOne.setUrl("https://jingyan.baidu.com/article/a3f121e4d3f752fc9052bb90.html");

                WxMpXmlOutNewsMessage.Item itemTwo = new WxMpXmlOutNewsMessage.Item();
                itemTwo.setTitle("hi how aldfjlakflk you");
                itemTwo.setDescription("this is asdkfkaslkf demo demo");
                itemTwo.setPicUrl("https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1510307158&di=258b895c1f6840f2b2af3802f6493b66&src=http://imgsrc.baidu.com/imgad/pic/item/9213b07eca806538025ec1349ddda144ad348264.jpg");
                itemTwo.setUrl("https://jingyan.baidu.com/article/a3f121e4d3f752fc9052bb90.html");

                articles.add(itemOne);
                articles.add(itemTwo);

                return  new NewsBuilder().build(articles , wxMessage);
            }

        } catch (WxErrorException e) {
            e.printStackTrace();
        }

        //TODO 组装回复消息
        String content = "收到信息内容：" + JsonUtils.toJson(wxMessage);

        return new TextBuilder().build(content, wxMessage);

    }

}
